package com.egg.common.util;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Enumeration;

/**
 * 请求工具类
 *
 * @author east
 */
@Slf4j
public class HttpServletUtil {


    public static ServletRequestAttributes getServletRequestAttributes() {
        try {
            // 这种方式获取的HttpServletRequest是线程安全的
            return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        } catch (Exception e) {
            return null;
        }
    }

    public static HttpServletRequest getHttpServletRequest() {
        try {
            // 这种方式获取的HttpServletRequest是线程安全的
            ServletRequestAttributes servletRequestAttributes = getServletRequestAttributes();
            if (null == servletRequestAttributes) {
                return null;
            }
            return servletRequestAttributes.getRequest();
        } catch (Exception e) {
            return null;
        }
    }

    public static HttpServletResponse getHttpServletResponse() {
        try {
            // 这种方式获取的HttpServletRequest是线程安全的
            ServletRequestAttributes servletRequestAttributes = getServletRequestAttributes();
            if (null == servletRequestAttributes) {
                return null;
            }
            return servletRequestAttributes.getResponse();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 解析请求参数
     *
     * @param point
     * @return
     */
    public static JSONObject parsingRequestParam(ProceedingJoinPoint point) {
        JSONObject jsonParam = new JSONObject();
        if (null == point) {
            return jsonParam;
        }
        Signature signature = point.getSignature();
        // 处理请求参数
        String[] paramNames = ((MethodSignature) signature).getParameterNames();
        if (null == paramNames || 0 == paramNames.length) {
            return jsonParam;
        }
        Object[] paramValues = point.getArgs();
        int index = paramNames.length - 1;
        for (int i = 0; i < index; i++) {
            Object value = paramValues[i];
            if (ObjectUtil.isEmpty(value)) {
                continue;
            }
            String key = paramNames[i];
            jsonParam.put(key, value);
        }
        if (null == paramValues[index]) {
            return jsonParam;
        }
        if (paramValues[index] instanceof ServletResponse) {
            return jsonParam;
        }
        if (paramValues[index] instanceof Collection) {
            jsonParam.put(paramNames[index], paramValues[index]);
            return jsonParam;
        }
        if (JSONUtil.isJson(JSON.toJSONString(paramValues[index]))) {
            jsonParam.putAll(JSON.parseObject(JSON.toJSONString(paramValues[index])));
            return jsonParam;
        }
        return jsonParam;
    }

    /**
     * 解析请求头
     *
     * @param request
     * @return
     */
    public static JSONObject parsingRequestHeader(HttpServletRequest request) {
        if (null == request) {
            return new JSONObject();
        }
        JSONObject jsonHeader = new JSONObject();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (null == headerNames) {
            return jsonHeader;
        }
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            jsonHeader.put(name, request.getHeader(name));
        }
        return jsonHeader;
    }


}
